package com.dingding.service.user.core.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.UserAttached;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.service.user.core.dao.IUserAttachedDao;
import com.dingding.service.user.core.sql.UserAttachedSQL;

/**
 * 
 * @ClassName: IUserAttachedDao 
 * @Description:用户属性附属表操作DAO
 * @author CHEHAUNBO 
 * @date 2016年4月1日 下午2:27:22
 * 
 */
@Component("userAttachedDao")
public class UserAttachedDaoImpl extends BaseDaoImpl<UserAttached> implements IUserAttachedDao {

	private static final Logger log = Logger.getLogger(UserAttachedDaoImpl.class);

	/**
	 * 
	 * @Title: getUserAttached 
	 * @Description: 获取用户附属属性值
	 * @param userId 用户ID
	 * @param key 
	 * @return UserAttached  查询到的用户属性对象
	 * @author CHEHAUNBO
	 * 
	 */
	@Override
	@Transactional(readOnly = true)
	public UserAttached getUserAttached(String userId, String key) {

		List<UserAttached> userAttacheds = null;
		UserAttached attached = null;

		try {
			userAttacheds = super.listHql(UserAttachedSQL.GET_USER_ATTACHED, userId, key);
			if (userAttacheds != null && userAttacheds.size() > 0) {
				attached = userAttacheds.get(0);
			}
		} catch (Exception e) {
			log.error("查询用户附属属性值出现异常！", e);
		}

		return attached;
	}

	/**
	 * 
	 * @Title: isExistUserAttached
	 * @Description:根据用户ID及属性KEY，判断当前属性是否存在
	 * @param userId 用户ID
	 * @param key 属性KEY
	 * @return boolean true 成功 false 失败
	 * @author CHEHAUNBO
	 * 
	 */
	@Override
	@Transactional(readOnly = true)
	public boolean isExistUserAttached(String userId, String key) {

		List<UserAttached> userAttacheds = super.listHql(UserAttachedSQL.GET_USER_ATTACHED, userId, key);
		if (userAttacheds != null && userAttacheds.size() > 0) {
			return true;
		}

		return false;
	}

	/**
	 * 
	 * @Title: addUserAttached
	 * @Description:根据用户ID及属性KEY，添加用户属性
	 * @param userId 用户ID
	 * @param key 属性KEY
	 * @return boolean true 成功 false 失败
	 * @author CHEHAUNBO
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean addUserAttached(String userId, String key) {

		UserAttached attached = new UserAttached();
		// 主键ID
		attached.setUserAttachedId(ToolUtil.getUid());
		// 用户ID
		attached.setLoginId(userId);
		// 属性key
		attached.setAttachedkey(key);
		// 属性value
		attached.setAttachedkeyValue(1);

		try {
			super.save(attached);
		} catch (Exception e) {
			log.error("添加用户附属属性出现异常！", e);
			return false;
		}

		return true;
	}

	/**
	 * 
	 * @Title: updateUserAttached 
	 * @Description: 更新用户附属属性
	 * @param userId 用户ID
	 * @param key 属性KEY
	 * @param value 属性Value
	 * @return boolean  true 更新成功 false 更新失败
	 * @author CHEHAUNBO
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean updateUserAttached(String userId, String key, int value) {

		try {

			int row = super.updateSqlRow(UserAttachedSQL.UPDATE_USER_ATTACHED, value, userId, key);
			if (row > 0) { // 修改成功
				return true;
			}

		} catch (Exception e) {
			log.error("更新用户附属属性值出现异常！", e);
		}
		return false;
	}

	/**
	 * @Title: resetUserAttached
	 * @Description: 重置用户每日签到
	 * @author yuHaiQing
	 * @return boolean    返回类型
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean resetUserAttached() {

		try {
			// 执行重置操作，并返回受影响行数
			int row = super.updateSqlRow(UserAttachedSQL.RESET_USER_ATTACHED);

			if (row >= 0) { // 重置成功

				return true;
			}
		} catch (Exception e) {
			log.error("重置用户签到出现异常 类：" + this.getClass() + " resetUserAttached()", e);
		}

		return false;
	}

}
